package com.wkh.maker.generator;

import java.io.*;

/**
 * @author wkh
 */
public class JarGenerator {
    public static void doGenerator(String projectDir) throws IOException, InterruptedException {
        //清理之前的构建并打包
        //注意不同的操作系统，命令不同
//        String winMavenCommand = "mvn.cmd clean package -DskipTests=true";
        String otherMavenCommand = "mvn clean package -DskipTests=true";
        String mavenCommand = otherMavenCommand;
        //这里一定要拆分
        ProcessBuilder processBuilder = new ProcessBuilder(mavenCommand.split(" "));
        processBuilder.directory(new File(projectDir));

        Process process = processBuilder.start();

        //读取命令的输出
        InputStream inputStream = process.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ((line = bufferedReader.readLine()) != null){
            System.out.println(line);
        }
        //等待命令执行完成
        int existCode = process.waitFor();
        //1表示异常退出，0表示正常退出
        System.out.println("命令执行结束，退出码:"+existCode);
    }

    public static void main(String[] args) {
        try {
            doGenerator("D:\\code\\xinyuanyi-generator\\xinyuanyi-generator-maker\\generated\\acm-template-generator");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
